package com.gxwy.zizhanghu.controller;


import java.sql.Timestamp;
import java.util.Map;

import javax.servlet.http.HttpSession;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gxwy.zizhanghu.form.ZiZhangHu;
import com.gxwy.zizhanghu.service.*;


@Controller
@RequestMapping("/Zizhanghu")
public class ZizhanghuController {

	@Autowired

	private  ZiZhangHuService Service;


	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String listContact(Map<String, Object> map,
			String choose,HttpSession session,Map<String, Object> ma,
			String zhanghuleixing,String yinhangzhanghao) {
            session.getAttribute("user");
        
		if(choose==null){
			return  "redirect:/yonghuziliao/index1";
		}
   
		String[] accounts= choose.split(",");
 
 
  

		for(int i=0;i<accounts.length;i++){
			map.put("contact2"+String.valueOf(i),Service.list(accounts[i]));
			map.put("contactList"+String.valueOf(i),Service.listId(accounts[i]));
		
		}
//		for(int i=0;i<accounts.length;i++){
//           ma.put("contact",  contactService.list(accounts[i]));
//           }
//	  map.put("contactList"+String.valueOf(i), contactService.listId(accounts[i]));
        
		session.setAttribute("aa",map);
		return "zhyecx1";
  
	}

	@RequestMapping("/lists")
	public String listContacts(Map<String, Object> map,HttpSession session) {

		map.put("contact", new ZiZhangHu());
		map.put("contactList", Service.findAll());
      
		return "zizhanghu";
	}

	@RequestMapping(value = "/addC", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("contact")
	ZiZhangHu contact, BindingResult result,HttpSession session) {
        
		
		Service.add(contact);
		Timestamp loginTime=new Timestamp(System.currentTimeMillis());
        contact.setKaihuriqi(loginTime);
        return "redirect:lists";

	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@ModelAttribute("contact")
	ZiZhangHu contact, BindingResult result,String name,Map<String, Object> map)
	{
		System.out.println(name);
		if(name==""){
			 return "redirect:lists";		}
		
		Service.update(contact,Integer.valueOf(name));
	
		 return "redirect:lists";
	
	}

	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public String deleteContact(String delet) {
		if(delet==""){
	    return "redirect:lists";
			
		}
		Service.Delete(Integer.valueOf(delet));

		return  "redirect:lists";
	}

	@RequestMapping(value = "/select",method = RequestMethod.POST)

	
	public 	@ResponseBody Map<String, Object> getShopInJSON(@RequestParam(value = "a",required = true)String a,Map<String, Object> map) {
	
	map.put("message",Service.select(a));
	
	System.out.println(map);
	
	return map;
	    
		
	   }


}
